package com.imm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imm.common.Result;
import com.imm.mapper.AddressBookMapper;
import com.imm.pojo.AddressBook;
import com.imm.service.IAddressBookService;
import com.imm.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {
    @Override
    public Result<List<AddressBook>> listAddress(AddressBook addressBook) {
        // 获取用户Id
        Long userId = BaseContext.get();
        addressBook.setUserId(userId);
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, addressBook.getUserId());
        List<AddressBook> addressBooks = list(wrapper);
        return Result.success(addressBooks);
    }

    @Transactional
    @Override
    public Result<String> insert(AddressBook addressBook) {
        // 判断是否存在某认地址
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook one = getOne(wrapper);
        if (one == null) {
            addressBook.setIsDefault((short) 1);
        }
        Long userId = BaseContext.get();
        addressBook.setUserId(userId);
        save(addressBook);
        return Result.success("新增地址成功");
    }

    @Override
    public Result<String> defalut(AddressBook addressBook) {
        // 去除原先的默认地址
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AddressBook::getIsDefault, (short)0).eq(AddressBook::getIsDefault, (short)1);
        update(wrapper);
        // 更新为默认地址
        addressBook.setIsDefault((short)1);
        updateById(addressBook);
        return Result.success("设为默认地址成功");
    }

    @Override
    public Result<AddressBook> defalut() {
        Long userId = BaseContext.get();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault , 1);
        AddressBook one = getOne(wrapper);
        if (one == null) {
            return Result.error("没有默认地址");
        }
        return Result.success(one);
    }

    @Override
    public Result<AddressBook> findOne(Long id) {
        AddressBook addressBook = getById(id);
        return Result.success(addressBook);
    }

    @Transactional
    @Override
    public Result<String> delete(Long[] ids) {
        removeBatchByIds(List.of(ids));
        return Result.success("删除成功");
    }

    @Override
    public Result<String> updateAddressBook(AddressBook addressBook) {
        updateById(addressBook);
        return Result.success("修改成功");
    }


}
